package com.iseastar.guojiang.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.shouhuobao.station.R;

/**
 * 项目用到的带下划线的LineraLayout <br/>
 * 主要为了新增
 *
 * @author coffee <br>
 *         2016-11-15下午4:36:45
 */
public class LinearLayoutWithLine extends LinearLayout {

    private Paint mLinePaint;
    private int paddlingLineLeft;
    private int paddlingLineTop;
    private int paddlingLineRight;
    private int paddlingLineBottom;

    public LinearLayoutWithLine(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LinearLayoutWithLine(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (isInEditMode()) {
            return;
        }
        mLinePaint = new Paint();
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LayoutWithLine);
        paddlingLineLeft = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineLeft, 0);
        paddlingLineTop = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineTop, 0);
        paddlingLineRight = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineRight, 0);
        paddlingLineBottom = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineBottom, 0);
        int linecolor = ta.getColor(R.styleable.LayoutWithLine_lineColor, getResources().getColor(R.color.app_line_color));
        // 下划线的颜色
//		int linecolor = getResources().getColor(R.color.app_line_color);
        // 如果是8位的带透明度的色值,
        mLinePaint.setColor(linecolor);
        // 单位px
        int colorWidth = (int) getResources().getDimension(R.dimen.app_line_height);
        mLinePaint.setStrokeWidth(colorWidth);
        ta.recycle();
        // 设置false, 否则不执行 #onDraw
        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int top = paddlingLineTop;
        int bottom = getHeight() - paddlingLineBottom;
        int left = paddlingLineLeft;
        int right = getWidth() - paddlingLineRight;
        if (paddlingLineBottom > 0) { // 画底部的线
            canvas.drawLine(left, bottom, right, bottom, mLinePaint);
        }
        if (paddlingLineTop > 0) {// 顶部
            canvas.drawLine(left, top, right, top, mLinePaint);
        }
        if (paddlingLineLeft > 0) {// 左侧
            canvas.drawLine(left, top, left, bottom, mLinePaint);
        }
        if (paddlingLineRight > 0) {// 左侧
            canvas.drawLine(right, top, right, bottom, mLinePaint);
        }
    }

    public void setLinePadding(int left, int top, int right, int bottom) {
        this.paddlingLineLeft = left;
        this.paddlingLineTop = top;
        this.paddlingLineRight = right;
        this.paddlingLineBottom = bottom;
        this.invalidate();
    }
}
